// pages/signup/signup.js
import validate from '../../utils/validate.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,
    overtime:false,
    nowTime:'',
    lastTime:'',
    userInfo:{},
    sex_index:0,
    type_index:0,
    department_index:0,
    sexs:[
      {id:'女',text:'女'},
      {id:'男',text:'男'}
    ],
    types:[
      {id:'陪护',text:'陪护'},
      {id:'紧急探视',text:'紧急探视'},
      {id:'非本院工作人员',text:'非本院工作人员'},
    ],
    departments:[
      {id:'内一科',text:'内一科'},
      {id:'内二科',text:'内二科'},
      {id:'内三科',text:'内三科'},
      {id:'内四科',text:'内四科'},
      {id:'内五科',text:'内五科'},
      {id:'内六科',text:'内六科'},
      {id:'血透室',text:'血透室'},
      {id:'脑病综合科',text:'脑病综合科'},
      {id:'老年病科',text:'老年病科'},
      {id:'针灸推拿康复科',text:'针灸推拿康复科'},
      {id:'肿瘤综合科',text:'肿瘤综合科'},
      {id:'外一科',text:'外一科'},
      {id:'外二科',text:'外二科'},
      {id:'外三科',text:'外三科'},
      {id:'肛肠科',text:'肛肠科'},
      {id:'妇  科',text:'妇  科'},
      {id:'五官科',text:'五官科'},
      {id:'麻醉科/手术室',text:'麻醉科/手术室'},
      {id:'ICU',text:'ICU'},
      {id:'骨一科',text:'骨一科'},
      {id:'骨二科',text:'骨二科'},
      {id:'骨三科',text:'骨三科'},
    ]
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShow();
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  changeSex: function(event){
    this.setData({
      sex_index : event.detail.value
    })
  },
  changeType: function(event){
    this.setData({
      type_index : event.detail.value
    })
  },
  changeDepartment:function(event){
    this.setData({
      department_index : event.detail.value
    })
  },
  submit(event){
    let data = event.detail.value
    if(this.data.overtime && (data.code == undefined || data.code == '')){
      wx.showModal({
        title: '提示',
        content:  '验证码不能为空',
        showCancel:false
      })
      return;
    }else{
      this.setData({
        "userInfo.code" : data.code
      })
    }
    this.setData({
      "userInfo.phone" : data.phone,
      "userInfo.sex" :this.data.sexs[this.data.sex_index].id,
      "userInfo.username" :data.username,
      "userInfo.bedno" :data.bedno,
      "userInfo.idcard" :data.idcard,
      "userInfo.department" :this.data.departments[this.data.department_index].id,
      "userInfo.type" :this.data.types[this.data.type_index].id,
    })
    if(this.validateSubmit(this.data.userInfo)){
      app.api.saveUser(this.data.userInfo).then(res =>{
        wx.redirectTo({
          url: '../message/message',
        })
      })
    }
  },
  validateSubmit(obj){
    let msg = ''
    let phone_test=/^[1][3,4,5,7,8,9][0-9]{9}$/;
    let id_test = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if(validate.validatenull(obj.username)){
      msg = '用户姓名';
    }else if(this.data.isShow && !phone_test.test(obj.phone)){
      msg = '用户手机号格式错误或'
    }else if(validate.validatenull(obj.department)){
      msg = '用户探访科室';
    }else if(this.data.isShow && validate.validatenull(obj.bedno) ){
      msg = '床号';
    }else if(this.data.isShow && !id_test.test(obj.idcard)){
      msg = '用户身份证格式错误或';  
    }else{
      return true;
    }
    wx.showModal({
      title: '提示',
      content: msg + '不能为空',
      showCancel:false
    })
    return false;
  },
  getUser(){
    wx.login({
      success: res => {
       // 发送 res.code 到后台换取 openId, sessionKey, unionId
       app.api.login({Jscode : res.code}).then(res => {
        if(res.data.data.openId != null){
          this.getLastTask(res.data.data.openId);
        }
        //let now = this.getNowDate();
        let ind = 0;
        for(let i =0; i < this.data.departments.length; i ++ ){
          if(this.data.departments[i].text == res.data.data.department){
            ind = i
          }
        }
        this.setData({
          userInfo:res.data.data,
          sex_index:res.data.data.sex == '女'? 0:1,
          type_index:res.data.data.type == '陪护'? 0:1,
          department_index:ind,
          //nowTime : now,
        })
        if(res.data.data.id == null){
          wx.showModal({
            title: '提示',
            content: '您填写的信息将用于企业内部使用!',
            showCancel:false
          })
        }
        })
      }
    })
  },
  getNowDate(){
    let d = new Date();
    let vYear = d.getFullYear();
    let vMon = d.getMonth() + 1;
    let vDay = d.getDate();
    let h = d.getHours();
    let m = d.getMinutes();
    let se = d.getSeconds();
    let s=vYear +'年'+(vMon<10 ? "0" + vMon : vMon)+'月'+(vDay<10 ? "0"+ vDay : vDay)+'日 '+(h<10 ? "0"+ h : h)+'时'+(m<10 ? "0" + m : m)+'分';
    　return s;
    }
    ,
  getNowTime(){
    let d = new Date();
    let h = d.getHours();
    let m = d.getMinutes();
    let s=(h<10 ? "0"+ h : h)+":"+(m<10 ? "0" + m : m);
    　return s;
  },
  getShow(){
    app.api.getShow().then(res => {
        this.setData({
          isShow:res.data.show
        })
      })
  },
  getLastTask(openId){
    app.api.getEndTask({openId}).then(res => {
      if(res.data.data == null){
        this.setData({
          lastTime : this.getNowDate()
        })
        return;
      }
      let useTime = res.data.data.useTime ;
      let overtime = ('陪护' == res.data.data.type && useTime > 120 ) 
      this.setData({
        lastTime: res.data.data.endTime,
        time:useTime,
        overtime:overtime
      })
      if(overtime){
        wx.showModal({
          title: '提示',
          mask:true,
          content: '外出超时!',
          showCancel:false
        })
        wx.vibrateShort({
          type:'medium'
        })
      }
 
    })
  },
  showHisoty(){
    wx.navigateTo({
      url: '../history/history',
    })
  }
})